package com.ruoyi.web.controller.bookkeeping;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.bookkeeping.domain.param.AddBookDynamicParam;
import com.ruoyi.bookkeeping.domain.param.PageQueryParam;
import com.ruoyi.bookkeeping.domain.param.UpdateBookDynamicParam;
import com.ruoyi.bookkeeping.domain.vo.BookDynamicVo;
import com.ruoyi.bookkeeping.domain.vo.MsgResult;
import com.ruoyi.bookkeeping.domain.vo.TableDataInfoVo;
import com.ruoyi.bookkeeping.util.GetPageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bookkeeping.service.IBookDynamicService;


/**
 * 动态Controller
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@RestController
@RequestMapping("/bookkeeping/dynamic")
@Api(tags = "动态模块")
public class BookDynamicController extends BaseController
{
    @Autowired
    private IBookDynamicService bookDynamicService;

    @GetMapping("/list")
    @ApiOperation("获取动态列表")
    public TableDataInfoVo<List<BookDynamicVo>> list(PageQueryParam pageQuery)
    {
        Page page = GetPageUtil.getPages(pageQuery.getPageNum(),pageQuery.getPageSize(),bookDynamicService.selectBookDynamicList(getUserId()));
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @GetMapping(value = "/{dynamicId}")
    public MsgResult getInfo(@PathVariable("dynamicId") Long dynamicId)
    {
        return success(bookDynamicService.selectBookDynamicByDynamicId(dynamicId));
    }

    @Log(title = "动态", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增动态")
    public MsgResult add(@RequestBody AddBookDynamicParam addBookDynamicParam)
    {
        return toAjax(bookDynamicService.insertBookDynamic(addBookDynamicParam,getUserId()));
    }

    @Log(title = "动态", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改动态")
    public MsgResult edit(@RequestBody UpdateBookDynamicParam updateBookDynamicParam)
    {
        return toAjax(bookDynamicService.updateBookDynamic(updateBookDynamicParam,getUserId()));
    }

    @Log(title = "动态", businessType = BusinessType.DELETE)
    @ApiOperation("删除动态")
	@DeleteMapping("/{dynamicId}")
    public MsgResult remove(@PathVariable Long dynamicId)
    {
        return toAjax(bookDynamicService.deleteBookDynamicByDynamicId(dynamicId,getUserId()));
    }

    @PutMapping("/{dynamicId}")
    @ApiOperation("点赞或取消点赞动态")
    public MsgResult goodBookDynamic(@PathVariable Long dynamicId, Long goodOrCancle)
    {
        return toAjax(bookDynamicService.goodBookDynamic(dynamicId,getUserId(),goodOrCancle));
    }

}
